package com.cat.greedyAlgorithm;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/maximum-swap/?envType=problem-list-v2&envId=greedy
 * @create 2025/10/3 18:21
 * @since JDK17
 */

public class Solution45 {
    void swap(char[] s, int i, int j) {
        char t = s[i];
        s[i] = s[j];
        s[j] = t;
    }

    public int maximumSwap(int num) {
        List<String> list = new ArrayList<>();
        char[] array = String.valueOf(num).toCharArray();
        list.add(new String(array));
        int n = array.length;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                swap(array, i, j);
                list.add(new String(array));
                swap(array, i, j);
            }
        }
        list.sort((a, b) -> a.compareTo(b));
        return Integer.valueOf(list.get(0));
    }
}
